<template>
	<view>
		<uv-cell-group>
			<uv-cell v-for="(item, itemIndex) in detailItems" :key="itemIndex"
				:title="item.title" :value="item.value" 
				isLink @click.capture="item.onClick">
			</uv-cell>
		</uv-cell-group>
		<uv-button class="uni-mt-16" text="删除档案" type="error"
			@click="onDeletePetInfo"></uv-button>
	</view>
</template>

<script lang="ts" setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref, ComputedRef, computed } from 'vue'
	
	const db = uniCloud.databaseForJQL()
	const petInfo = ref(null)
	onLoad(async (option) => {
		petInfo.value = (await db.collection('pet-pets').doc(option.id).get()).data[0]
	})
	
	interface DetailItem {
		title: string,
		value: string | ComputedRef<string>,
		onClick?: () => void
	}
	const detailItems = ref<DetailItem[]>([
		{
			title: '宠物名称',
			value: computed(() => petInfo.value ? petInfo.value.name : ''),
		},
		{
			title: '宠物种类',
			value: computed(() => petInfo.value ? petInfo.value.type : ''),
		},
	])
	
	const onDeletePetInfo = () => {
		uni.showModal({
			title: '删除档案',
			content: '删除后无法恢复，确定删除吗？',
			success: async (res) => {
				if (petInfo.value && res.confirm) {
					uni.showLoading({
						title: '删除中'
					})
					await db.collection('pet-pets').doc(petInfo.value._id).update({
						visibility_status: 2
					})
					uni.hideLoading()
					uni.navigateBack()
				}
			}
		})
	}
	
</script>

<style>

</style>
